package com.zhihaoliang.coroutine.buss.concurrent.func

import com.zhihaoliang.coroutine.buss.concurrent.massiveRun
import com.zhihaoliang.coroutine.util.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlin.system.measureTimeMillis

/**
 * @author 支豪亮
 * @date 2022/10/31 15:22
 * @des 并行错误
 */
object CurrentErro {

    fun showErro(mainScope: CoroutineScope) = mainScope.launch(Dispatchers.Default) {
        var counter = 0
        massiveRun {
            counter++
        }
        log("Counter = $counter")
    }


    @Volatile // in Kotlin `volatile` is an annotation
    var counterVolatile = 0
    fun showVolatilesErro(mainScope: CoroutineScope) = mainScope.launch(Dispatchers.Default) {
         counterVolatile = 0
        massiveRun {
            counterVolatile++
        }
        log("counterVolatile = $counterVolatile")
    }
}